﻿using ReflectionStudio.Core.Events;
using System;
using System.ComponentModel;
using System.Data;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using WPFExcelReport.Helpers;

namespace WPFExcelReport
{
    public partial class TIVC : ViewBase
    {
       
        #region ==dwTIVC===========
        public static readonly DependencyProperty dwTIVCProperty = DependencyProperty.Register("dwTIVC",
            typeof(DataRow), typeof(TIVC),
            new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OndwTIVCChanged));
        public DataRow dwTIVC
        {
            get
            {
                return (DataRow)GetValue(dwTIVCProperty);
            }
            set
            {
                SetValue(dwTIVCProperty, value);
            }
        }
        private static void OndwTIVCChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as TIVC;
            DataRow row = (DataRow)e.NewValue;
            if (src != null)
            {
                src.dwTIVC = row;
            } 
        }
        #endregion
        #region ==FROMDATE===========
        public static readonly DependencyProperty FROMDATEProperty = DependencyProperty.Register("FROMDATE",
            typeof(string), typeof(TIVC),
            new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnFROMDATEChanged));
        public string FROMDATE
        {
            get
            {
                return (string)GetValue(FROMDATEProperty);
            }
            set
            {
                SetValue(FROMDATEProperty, value);
            }
        }
        private static void OnFROMDATEChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as TIVC;
            if (src != null)
            {
                src.FROMDATE = (string)e.NewValue;
            }
        }
        #endregion
        #region ==ENDDATE===========
        public static readonly DependencyProperty ENDDATEProperty = DependencyProperty.Register("ENDDATE",
            typeof(string), typeof(TIVC),
            new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnENDDATEChanged));
        public string ENDDATE
        {
            get
            {
                return (string)GetValue(ENDDATEProperty);
            }
            set
            {
                SetValue(ENDDATEProperty, value);
            }
        }
        private static void OnENDDATEChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as TIVC;
            if (src != null)
            {
                src.ENDDATE = (string)e.NewValue;
            }
        }
        #endregion
        VVKPOS GB;
        ITXEXRPT ITXEXRPT;
        DataSet Return;
        public TIVC()
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return;
            MV = new VICC_VKCOST();
            GB = new VVKPOS();
            IsMasterFromMV = true;
            InitializeComponent();
           
        }
        public override void Window_Loaded(object sender, RoutedEventArgs e)
        {            
            base.Window_Loaded(sender, e);
            txtFACTORY.Focus();
        }        
        private void txtART_KeyUp(object sender, System.Windows.Input.KeyEventArgs e)
        {
            
            if (e.Key == Key.Enter)
            {
                this.Focus();
                QueryCommand(null, null);
                txtFACTORY.Focus();
            }
        }
        public override void QueryCommand(object sender, System.Windows.RoutedEventArgs e)
        {
            try
            {

                DataSet Result;
                Return = new DataSet();
                DataRow dwData = null;
                string ART = txtART.Text.ToString().Trim();
                string INV = txtINV.Text.ToString().Trim();
                string FACTORY = txtFACTORY.Text.ToString().Trim();
                string CURR = txtCURR.Text.ToString().Trim();
                if (FROMDATE == "")
                {
                    FROMDATE = "20000000";
                }
                if (ENDDATE == "")
                {
                    ENDDATE = "99999999";
                }
                if (FACTORY == "")
                {
                    FACTORY = "XXXXX";
                }
                if (CURR == "")
                {
                    CURR = "XXXXX";
                }
                if (INV == "")
                {
                    INV = "XXXXX";
                }
                if (ART == "")
                {
                    ART = "XXXXX";
                }

                dwData = GB.SetProd.Tables["VKPOS"].NewRow();
                dwData["VKPART"] = ART;
                dwData["VKPNEUDAT"] = FROMDATE;
                dwData["VKPAENDDAT"] = ENDDATE;
                dwData["VKPWAEHRSL"] = CURR;
                dwData["VKPKOLL"] = FACTORY;
                dwData["VKPKDBEZ"] = INV;

                Result = ((VICC_VKCOST)MV).QueryICC_VKCOST(dwData, 3);               
                //MergeData(Result, false);
                vwMaster.Source = Result.Tables["ICC_VKCOST"];
                Return.Merge(Result);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }

            base.QueryCommand(sender, e);
        }

        public override void vwMaster_CurrentChanged(object sender, EventArgs e)
        {
            base.vwMaster_CurrentChanged(sender, e);
            dwTIVC = dwMaster;
        }

        private void PrintCommand(object sender, RoutedEventArgs e)
        {
            try
            {
                if (ITXEXRPT == null)
                {
                    ITXEXRPT = new ITXEXRPT();
                    ITXEXRPT.Root = "C:\\IntexReport";
                    ITXEXRPT.strCompany = app.company;
                    ITXEXRPT.PrintType = "OpenExcel";
                }
                ITXEXRPT.dsSource = Return;
                //ITXEXRPT.dsSource.Merge(SetEnv);

                ITXEXRPT.DocType = "JLTVKCOST";

                ITXEXRPT.strExcelFile = @"\JLT\JLTView.xls";
                string filename = ITXEXRPT.WriteExcelReport();
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }

        private void SaveCommand(object sender, RoutedEventArgs e)
        {
            try
            {
                string ART = txtART.Text.ToString().Trim();
                string INV = txtINV.Text.ToString().Trim();
                string FACTORY = txtFACTORY.Text.ToString().Trim();
                string CURR = txtCURR.Text.ToString().Trim();
                string PROD = txtPRODVALUE.Text.ToString().Trim();
                string OTHERS = txtOTHERS.Text.ToString().Trim();
                if (FROMDATE == "")
                {
                    FROMDATE = "20000000";
                }
                if (ENDDATE == "")
                {
                    ENDDATE = "99999999";
                }
                if (FACTORY == "")
                {
                    FACTORY = "XXXXX";
                }
                if (CURR == "")
                {
                    CURR = "XXXXX";
                }
                if (INV == "")
                {
                    INV = "XXXXX";
                }
                if (ART == "")
                {
                    ART = "XXXXX";
                }
                if (PROD == "")
                {
                    PROD = "0";
                }
                if (OTHERS == "")
                {
                    OTHERS = "0";
                }

                dwData = GB.SetProd.Tables["VKPOS"].NewRow();
                dwData["VKPART"] = ART;
                dwData["VKPNEUDAT"] = FROMDATE;
                dwData["VKPAENDDAT"] = ENDDATE;
                dwData["VKPWAEHRSL"] = CURR;
                dwData["VKPKOLL"] = FACTORY;
                dwData["VKPKDBEZ"] = INV;
                dwData["VKPMENGE1"] = PROD;
                dwData["VKPMENGE2"] = OTHERS;
                ((VICC_VKCOST)MV).UpdateICC_VKCOST(dwData, "JLT1");
                txtPRODVALUE.Text = "";
                txtOTHERS.Text = "";
                QueryCommand(null,null);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        
        private void FROM_DATE_SelectedDateChanged(object sender, SelectionChangedEventArgs e)
        {
            try
            {
                FROMDATE = Helpers.Helper.GetDate(sender);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }

        }

        private void END_DATE_SelectedDateChanged(object sender, SelectionChangedEventArgs e)
        {
            try
            {
                ENDDATE = Helpers.Helper.GetDate(sender);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }

        }
    }
}
